package cn.hfuu.socket;

import java.io.*;
import java.net.Socket;

/**
 * @author oldliu
 * @since 1.0
 */
public class ChatThread extends Thread {
    private Socket s;

    public ChatThread(Socket s) {
        this.s = s;
    }

    @Override
    public void run() {
        System.out.println(s);//对方的ip,对方的端口（随机，30000+）
        try {
            OutputStream out = s.getOutputStream();
            InputStream in = s.getInputStream();
            //???... 转换成字符流，用 \n表示结束
            PrintWriter pw = new PrintWriter(out, true);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            //采用问答！
            String ask = "";
            while (true) {
                ask = br.readLine();//
                pw.println("你问的问题，[" + ask + "],朕知道了");
                //>>pw.flush();
                if (ask.equals("byebye")) {
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                System.out.println(s + "退出了");
                s.close();
            } catch (Exception e) {

            }
        }

    }
}
